home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / MacPerl 5.1.3 / Mac_Perl_513_src / MacPerl5 / BuildHelpIndex next >
Encoding:
Text File  |  1996-08-21  |  4.8 KB  |  196 lines  |  [TEXT/MPS ]

  1. Perl -Sx "{0}" {"Parameters"}
  2. Exit 0
  3.  
  4. #!perl -s
  5.  
  6. ($index = shift @ARGV) || die "No help index specified";
  7.  
  8. dbmopen(%INDEX, $index, 0666) || die "Couldn't open index file \"$index\"";
  9.  
  10. for (keys %INDEX) {
  11.     delete $INDEX{$_};
  12. }
  13.  
  14. while (<>) {
  15.     if (/\@menu/) {
  16.         &DoMenu();
  17.     } elsif (/\@index\s+(\S+\.pod)/) {
  18.         &DoPodIndex($1, $');
  19.     } elsif (/\@index\s+(\S+)/) {
  20.         &DoIndex($1, $');
  21.     } elsif (/\@file\s+(\S+)\s+(\S+)/) {
  22.         $INDEX{$1} = $2;
  23.     } elsif (/\S+/) {
  24.         die "Don't understand \"$&\"";
  25.     }
  26. }
  27.  
  28. if ($v) {
  29.     print "-----\n";
  30.     print $MENU;
  31.     print "-----\n";
  32.  
  33.     for (sort keys %INDEX) {
  34.         printf("%-20s %s\n", $_, $INDEX{$_});
  35.     }
  36. }
  37.  
  38. $INDEX{" MENU"} = $MENU;
  39. MacPerl::SetFileInfo('McPL', 'HELP', $index);
  40.  
  41. dbmclose %INDEX;
  42.  
  43. sub DoMenu {
  44.     $submenu = 0;
  45.     while (<>) {
  46.         if (/\@sep/) {
  47.             $MENU .= "-(\t\n";
  48.         } elsif (/\@sub\s+(\S.*\S)/) {
  49.             $MENU .= "$1\t!\n";
  50.             ++$submenu;
  51.         } elsif (/\@end/) {
  52.             $MENU .= "\n";
  53.             if (!$submenu--) {
  54.                 return;
  55.             }
  56.         } elsif (/(\S+)\s+(\S.*\S)/) {
  57.             $MENU .= "$2\t$1\n";
  58.         } elsif (/\S+/) {
  59.             die "Don't understand \"$&\"";
  60.         }
  61.     }
  62.     die "End of file while processing \@menu";
  63. }
  64.  
  65. sub DoIndex {
  66.     local($file, $var) = @_;
  67.     local(%rename, %alias);
  68.     
  69.     $url = $file;
  70.     if ($url =~ /^:/) {
  71.         $url = $';
  72.         $url =~ s|:|/|g;
  73.         $url = "file:$url";
  74.     } else {
  75.         $url =~ s|:|/|g;        
  76.         $url = "file://$url";
  77.     }
  78.     open(INDEX, $file) || die "Can't open index file \"$file\"";
  79.     $var = ($var =~ /\@var/);
  80.     while (<>) {
  81.         if (/\@end/) {
  82.             goto startindex;
  83.         } elsif (/\@index/) {
  84.             die "Forgot \@end";
  85.         } elsif (/\@rename\s+(\S+)\s+(\S+)/) {
  86.             warn "\"$2\" renamed both to \"$rename{$2}\" and \"$1\"" if $rename{$2};
  87.             $rename{$2} = $1;
  88.         } elsif (/\@alias\s+(\S+)\s+(\S+)/) {
  89.             warn "\"$1\" already aliased to \"$alias{$1}\"" if $alias{$1};
  90.             $alias{$1} = $2;
  91.         } elsif (/\@delete\s+(\S+)/) {
  92.             warn "\"$1\" both renamed to \"$rename{$1}\" and deleted" if $rename{$1};
  93.             $rename{$1} = "";
  94.         }
  95.     }
  96.     die "End of file while processing \@index";
  97. startindex:
  98.     while (<INDEX>) {
  99.         next unless /NAME\s*=\s*\"([^"]+)\"/;
  100.         ($name,$_) = ($1, $');
  101.         $key = $name;
  102.         $key =~ s/^perl[a-z]*_//;
  103.         if ($key =~ /_(\d+)/) {
  104.             redo unless !$1;    # Ignore all except _0
  105.             $key = $`;
  106.         }
  107.         $key = "\$\U$key\E" if $var;
  108.         $key = $rename{$key} if defined $rename{$key};
  109.         next unless $key;
  110.         warn "\"$key\" already defined as \"$INDEX{$key}\", redefined as \"$url#$name\""
  111.             if ($INDEX{$key} && $INDEX{$key} ne "$url#$name");
  112.         $INDEX{$key} = "$url#$name";
  113.     }
  114.     while (($key,$value) = each(%alias)) {
  115.         warn "\"$key\" already defined as \"$INDEX{$key}\", redefined as \"$INDEX{$value}\""
  116.             if ($INDEX{$key} && $INDEX{$key} ne $INDEX{$value});
  117.         warn "\"$value\" undefined" unless $INDEX{$value};
  118.         $INDEX{$key} = $INDEX{$value};
  119.     }
  120. }
  121.  
  122. sub DoPodIndex {
  123.     local($file, $var) = @_;
  124.     local(%rename, %alias);
  125.     
  126.     $url = $file;
  127.     if ($url =~ /^:/) {
  128.         $url = $';
  129.         $url =~ s|:|/|g;
  130.         $url = "pod:$url";
  131.     } else {
  132.         $url =~ s|:|/|g;        
  133.         $url = "pod://$url";
  134.     }
  135.     open(INDEX, $file) || die "Can't open index file \"$file\"";
  136.     while (<>) {
  137.         if (/\@end/) {
  138.             goto startindex;
  139.         } elsif (/\@index/) {
  140.             die "Forgot \@end";
  141.         } elsif (/\@rename\s+(\S+)\s+(.*\S)/) {
  142.             warn "\"$2\" renamed both to \"$rename{$2}\" and \"$1\"" if $rename{$2};
  143.             $rename{$2} = $1;
  144.         } elsif (/\@alias\s+(\S+)\s+(.*\S)/) {
  145.             warn "\"$1\" already aliased to \"$alias{$1}\"" if $alias{$1};
  146.             $alias{$1} = $2;
  147.         } elsif (/\@delete\s+(.*\S)/) {
  148.             warn "\"$1\" both renamed to \"$rename{$1}\" and deleted" if $rename{$1};
  149.             $rename{$1} = "";
  150.         }
  151.     }
  152.     die "End of file while processing \@index";
  153. startindex:
  154.     while (<INDEX>) {
  155.         if (/^=item\s+(.*\S)\s*$/) {
  156.             $name = $1;
  157.             next if ($name eq "*");
  158.             if ($name =~ /^\$([A-Za-z_]+|\^[A-Z]|<I<digit>>|.)(.?)/) {
  159.                 if ($2 eq "{") {
  160.                     $key = "\%$1";
  161.                 } elsif ($2 eq "[") {
  162.                     $key = "\@$1";
  163.                 } else {
  164.                     $key = "\$$1";
  165.                 }
  166.             } elsif ($name =~ /^([$@%](?:[A-Za-z_]+|\^[A-Z]|<I<digit>>|.))/) {
  167.                 $key = $1;
  168.             } elsif ($name =~ /^([A-Za-z0-9_:]+|(?:-[A-Za-z]))/) {
  169.                 $key = $1;
  170.             } elsif ($name =~ /^([^( ]+)/) {
  171.                 $key = $1;
  172.             } else {
  173.                 next;
  174.             }
  175.         } elsif (/^=head\d\s+(.*\S)\s*$/) {
  176.             $name = $1;
  177.             next if $name =~ /^[A-Z]+$/ && !defined $rename{$name}; # Skip NAME etc.
  178.             $key = $name;
  179.         } else {
  180.             next;
  181.         }
  182.         $name =~ s/([^a-zA-Z0-9_])/sprintf("%%%02X",unpack("c",$1))/ge;
  183.         $key = $rename{$key} if defined $rename{$key};
  184.         next unless $key;
  185.         warn "\"$key\" already defined as \"$INDEX{$key}\", trying to redefine as \"$url#$name\""
  186.             if ($INDEX{$key} && $key ne $lastkey && $INDEX{$key} ne "$url#$name");
  187.         $INDEX{$lastkey = $key} ||= "$url#$name";
  188.     }
  189.     while (($key,$value) = each(%alias)) {
  190.         warn "\"$key\" already defined as \"$INDEX{$key}\", trying to redefine as \"$INDEX{$value}\""
  191.             if ($INDEX{$key} && $key ne $lastkey && $INDEX{$key} ne $INDEX{$value});
  192.         warn "\"$value\" undefined" unless $INDEX{$value};
  193.         $INDEX{$lastkey = $key} ||= $INDEX{$value};
  194.     }
  195. }
  196.